package Tools.Logger;

import atag.gui.MainFrame;

import java.io.PrintStream;
import java.util.Properties;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.LogManager;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

/**
 * Created by IntelliJ IDEA.
 * User: leontiad
 * Date: Mar 9, 2010
 * Time: 3:58:46 PM
 * To change this template use File | Settings | File Templates.
 */
public class LogWrapper {
    private  Properties props = new Properties();


    public LogWrapper(Properties props) {
        this.props = props;
    }

    public Properties getProps() {
        return props;
    }

    public void setProps(Properties props) {
        this.props = props;
    }


    public void init() throws Exception {

        // initialize logging to go to rolling log file
        LogManager logManager = LogManager.getLogManager();
        logManager.reset();

        // log file max size 10K, 3 rolling files, append-on-open
        Handler fileHandler = new FileHandler(System.getProperty("user.home")+System.getProperty("file.separator")+props.getProperty("log"), 100000, 3, true);
        fileHandler.setFormatter(new SimpleFormatter());
        Logger.getLogger("").addHandler(fileHandler);

        // preserve old stdout/stderr streams in case they might be useful
        PrintStream stdout = System.out;
        PrintStream stderr = System.err;

        // now rebind stdout/stderr to logger
        Logger logger;
        LoggingOutputStream los;

        logger = Logger.getLogger("stdout");
        los = new LoggingOutputStream(logger, StdOutErrLevel.STDOUT);
        System.setOut(new PrintStream(los, true));

        logger = Logger.getLogger("stderr");
        los = new LoggingOutputStream(logger, StdOutErrLevel.STDERR);
        System.setErr(new PrintStream(los, true));        


    }

}

